package factory.service.Impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import factory.mapper.InterfaceMapper;
import factory.service.InterfaceService;
import interfaceManageInfo.domain.InterfaceManageInfo;
import interfaceManageInfo.dto.ConditionPageDTO;
import interfaceManageInfo.dto.InterfaceManageInfoDTO;
import interfaceManageInfo.vo.InterfaceManageInfoVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import result.PageDTO;
import utils.BeanConverter;
import java.util.Date;

@Service
public class InterfaceServiceImpl extends ServiceImpl<InterfaceMapper, InterfaceManageInfo>
                                implements InterfaceService {

    @Resource
    private InterfaceMapper interfaceMapper;
    @Override
    public Boolean updateStatic(Integer id) {
        InterfaceManageInfo interfaceManageInfo = interfaceMapper.selectById(id);
        switch (interfaceManageInfo.getApiState()){
            case 0, 2:
                interfaceManageInfo.setApiState(1);break;
            case 1:
                interfaceManageInfo.setApiState(2);break;
        }
         if(interfaceMapper.updateById(interfaceManageInfo) !=0){
             return true;
         }else {
             return false;
         }
    }

    @Override
    public Page<InterfaceManageInfoVO> pageByCondition(ConditionPageDTO conditionPageDTO) {
        LambdaQueryWrapper<InterfaceManageInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InterfaceManageInfo::getSource,conditionPageDTO.getSource())
                .eq(InterfaceManageInfo::getApiState,conditionPageDTO.getApiState())
                .like(InterfaceManageInfo::getInterfaceName,conditionPageDTO.getInterfaceName());
        Page<InterfaceManageInfo> interfacePage = new Page<>(conditionPageDTO.getPageNumber(), conditionPageDTO.getPageSize());
        Page<InterfaceManageInfo> page = this.page(interfacePage, wrapper);
        Page<InterfaceManageInfoVO> infoVOPage = new Page<>();
        infoVOPage.setRecords(BeanConverter.convertList(page.getRecords(),InterfaceManageInfoVO.class));
        infoVOPage.setTotal(page.getTotal());
        infoVOPage.setCurrent(page.getCurrent());
        infoVOPage.setSize(page.getSize());
        return infoVOPage;
    }

    @Override
    public Page<InterfaceManageInfo> pageListVO(PageDTO pageDTO) {
        Page<InterfaceManageInfo> interfaceManageInfoPage = new Page<>();
        return this.page(interfaceManageInfoPage);

    }

    @Override
    public Boolean saveInterface(InterfaceManageInfoDTO interfaceManageInfoDTO) {
        InterfaceManageInfo newInterfaceManageInfo = new InterfaceManageInfo();
        newInterfaceManageInfo.setInterfaceName(interfaceManageInfoDTO.getInterfaceName());
        newInterfaceManageInfo.setInterfaceDesc(interfaceManageInfoDTO.getInterfaceDesc());
        newInterfaceManageInfo.setInterfaceType(interfaceManageInfoDTO.getInterfaceType());
        newInterfaceManageInfo.setSource(interfaceManageInfoDTO.getSource());
        newInterfaceManageInfo.setApiState(interfaceManageInfoDTO.getApiState());
        newInterfaceManageInfo.setAgreement(interfaceManageInfoDTO.getAgreement());
        newInterfaceManageInfo.setIp(interfaceManageInfoDTO.getIp());
        newInterfaceManageInfo.setPath(interfaceManageInfoDTO.getPath());
        newInterfaceManageInfo.setRequestMethod(interfaceManageInfoDTO.getRequestMethod());
        newInterfaceManageInfo.setTimeout(interfaceManageInfoDTO.getTimeout());
        newInterfaceManageInfo.setCreateTime(new Date());
        newInterfaceManageInfo.setUpdateTime(new Date());
        newInterfaceManageInfo.setInValue(JSONUtil.toJsonStr(interfaceManageInfoDTO.getInValue()));
        newInterfaceManageInfo.setOutValue(JSONUtil.toJsonStr(interfaceManageInfoDTO.getOutValue()));
        newInterfaceManageInfo.setRequestBody(JSONUtil.toJsonStr(interfaceManageInfoDTO.getRequestBody()));


        if(this.save(newInterfaceManageInfo)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public InterfaceManageInfo oneInterface(Integer id) {
        return this.getById(id);
    }

    @Override
    public Boolean updateInterface(InterfaceManageInfoDTO interfaceManageInfoDTO) {
        InterfaceManageInfo newInterfaceManageInfo = new InterfaceManageInfo();
        InterfaceManageInfo byId = this.getById(interfaceManageInfoDTO.getId());
        newInterfaceManageInfo.setId(interfaceManageInfoDTO.getId());
        newInterfaceManageInfo.setInterfaceName(interfaceManageInfoDTO.getInterfaceName());
        newInterfaceManageInfo.setInterfaceDesc(interfaceManageInfoDTO.getInterfaceDesc());
        newInterfaceManageInfo.setInterfaceType(interfaceManageInfoDTO.getInterfaceType());
        newInterfaceManageInfo.setSource(interfaceManageInfoDTO.getSource());
        newInterfaceManageInfo.setApiState(interfaceManageInfoDTO.getApiState());
        newInterfaceManageInfo.setAgreement(interfaceManageInfoDTO.getAgreement());
        newInterfaceManageInfo.setIp(interfaceManageInfoDTO.getIp());
        newInterfaceManageInfo.setPath(interfaceManageInfoDTO.getPath());
        newInterfaceManageInfo.setRequestMethod(interfaceManageInfoDTO.getRequestMethod());
        newInterfaceManageInfo.setTimeout(interfaceManageInfoDTO.getTimeout());
        newInterfaceManageInfo.setUpdateTime(new Date());
        newInterfaceManageInfo.setCreateTime(byId.getCreateTime());
        newInterfaceManageInfo.setInValue(JSONUtil.toJsonStr(interfaceManageInfoDTO.getInValue()));
        newInterfaceManageInfo.setOutValue(JSONUtil.toJsonStr(interfaceManageInfoDTO.getOutValue()));
        newInterfaceManageInfo.setRequestBody(JSONUtil.toJsonStr(interfaceManageInfoDTO.getRequestBody()));
        if(this.save(newInterfaceManageInfo)){
            return true;
        }else {
            return false;
        }
    }




}
